/**
 * Manages tracking, updating, and killing boids, emitters, predators, and their bodies.
 */

import { Boid } from "./Boid.js";
import { Point, Vector } from "./Vector.js";
import { random } from "game-random";
import { Emitter } from "./Emitter.js";
class Flock {
    flock: Boid[];
    emitters: Emitter[];
    constructor() {
        this.flock = [];
        this.emitters = [];
    }

    addBoid(boid: Boid) {
        this.flock.push(boid);
    }

    addEmitter(emitter: Emitter) {
        this.emitters.push(emitter);
    }

    /** 添加一个随机地点的 */
    addRandomEmitter(vec: Point, repel = false) {
        this.addEmitter(
            new Emitter(
                () => vec,
                random(3000, 6000),
                random(25, 150),
                "linear",
                repel
            )
        );
    }
    addRandomBoid(vec: Point) {
        const newBoid = new Boid(
            () => {
                const vector = new Vector(vec.x, vec.y);
                return vector.add(new Vector(random(-10, 10), random(-10, 10)));
            },
            3,
            10,
            7,
            40,
            200,
            20,
            new Vector(800, 600)
        );
        this.addBoid(newBoid);
    }
    /** 更新参数 */
    update() {
        // Update emitters.

        this.emitters.forEach((i) => i.update(this.flock));

        //Calculate new headings for all boids within the flock.
        this.flock.forEach((i) => {
            const newHeading = i.calculateNewHeading(this.flock);
            i.move(newHeading);
        });
    }
}

export { Flock };
